var installedVersion = getPackageVersion("com.tencent.mm");
var supportVersion = ["8.0.35"]

console.info('当前微信版本：' + installedVersion)
if (supportVersion.includes(installedVersion)) {
  toast('当前微信版本为：' + installedVersion + '，满足需求')
  runScript()
} else {
  console.error('当前微信版本为：' + installedVersion + '，不满足需求，脚本停止')
  toast('当前微信版本为：' + installedVersion + '，不满足需求，脚本停止')
  exit()
}

function runScript() {
  begin();
  launchWechat();
  id('kd_').indexInParent(2).findOne(3000).click();
  console.info('打开发现');
  sleep(1000);
  id('iwg').desc('视频号').findOne(3000).click();
  console.info('打开视频号');
  sleep(2000);
  if (existsOne(text('我知道了'))) {
    console.info('需要点击我知道了');
    id('amj').findOne(3000).click();
  }
  sleep(3000);
  id('huj').findOne(3000).click();
  console.info('进入个人页面');
  id('h21').findOne(3000).click();
  console.info('进入视频号消息');
  desc('评论').findOne(3000).click();
  sleep(2000);
  console.info('进入评论列表');
  let loopCount = 0;
  while (loopCount < 5) {
    let followImageList = id('com.tencent.mm:id/hk8').visibleToUser(true).findOne(3000).children();
    followImageList.forEach((item, index) => {
      console.info('点击评论头像, index=%d', index);
      pickup(item, id('com.tencent.mm:id/a27')).click();
      sleep(3000);
      let popWindow = id('com.tencent.mm:id/ahb').findOne(5000);
      if (popWindow && popWindow.visibleToUser()) {
        console.info('发现普通微信用户，点击私信');
        wait(() => id('com.tencent.mm:id/jbm').findOne(5000).click());
      } else {
        console.info('发现视频号用户，点击私信');
        wait(() => id('com.tencent.mm:id/jb6').findOne(3000).click());
      }
      sleep(3000);
      console.info('判断是否已经发送过消息');
      const chatList = id('b79').findOne(5000);
      if (chatList.child(0).className() == 'android.widget.FrameLayout') {
        console.info('开始发送信息');
        const inputBox = className('android.widget.EditText').clickable(true).findOne(5000);
        inputBox.click();
        console.info('%replyContentType%');
        console.info('发送文字信息');
        inputBox.setText('%replyContentText%');
        className('android.widget.Button').text('发送').clickable(true).findOne(5000).click();
      }
      sleep(3000);
      className('android.widget.ImageView').desc('返回').findOne(5000).parent().click();
      wait('移入视频号黑名单');
      back();
      sleep(3000);
    })
    if (loopCount % 2 == 0) {
      scrollForward();
    } else {
      scrollForward();
      scrollForward();
    }
    sleep(3000);
    loopCount++;
  }
  
  end();
}

function launchWechat() {
  launch('com.tencent.mm');
  console.info('开始完全退出微信');
  waitForPackage('com.tencent.mm');
  backToHome();
  sleep(1000);
}

function backToHome() {
  let i = 0;
  while (!existsOne(id('f2s').text('通讯录')) || existsOne(id('text1').text('设置'))) {
    if (i >= 10) {
      launch('com.tencent.mm');
      console.info('开始完全退出微信');
      waitForPackage('com.tencent.mm');
    } else {
      back();
      sleep(1000);
      i++;
    }
  }
}

function begin() {
  device.wakeUp();
  device.keepScreenOn();
  auto();
}

function end() {
  toast('脚本执行结束');
  exit();
//   launch('com.timexxs.robot')
}

function getPackageVersion(packageName) {
  try {
      importPackage(android.content);
      var pckMan = context.getPackageManager();
      var packageInfo = pckMan.getPackageInfo(packageName, 0);
      return packageInfo.versionName;
  } catch (e) {
      dialogs.alert("您没有安装微信APP");
      exit();
  }
}
